package psn.polaris.db.init;

import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import psn.polaris.collection.CollectionUtils;
import psn.polaris.db.config.PolarisDataSourceDataInfo;
import psn.polaris.db.config.PolarisDataSourceInfo;
import psn.polaris.init.AbsPolarisOwnerStartProcess;
import psn.polaris.util.SpringUtils;

import javax.sql.DataSource;
import java.util.List;

@EnableConfigurationProperties({
        PolarisDataSourceInfo.class
})
@Component
@Configuration
public class PolarisDataSourceInit extends AbsPolarisOwnerStartProcess {

    @Autowired
    private PolarisDataSourceInfo polarisDataSourceInfo;

    public PolarisDataSourceInit() {
        super(-1);
    }

    /**
     * 初始化执行方法
     */
    @Override
    public void processInit() {
        int size = polarisDataSourceInfo.getDataDriveClass().size()-1;
        // 解析不同数据库的数据
        for (; size >= 0; size--) {
            PolarisDataSourceDataInfo dataInfo = new PolarisDataSourceDataInfo();
            dataInfo.setDataDriveClass(CollectionUtils.getItemFromList(polarisDataSourceInfo.getDataDriveClass(), size));
            dataInfo.setUrl(CollectionUtils.getItemFromList(polarisDataSourceInfo.getUrl(), size));
            dataInfo.setUser(CollectionUtils.getItemFromList(polarisDataSourceInfo.getUser(), size));
            dataInfo.setPwd(CollectionUtils.getItemFromList(polarisDataSourceInfo.getPwd(), size));
            dataInfo.setPoolClass(CollectionUtils.getItemFromList(polarisDataSourceInfo.getPoolClass(), size));
            dataInfo.setInitConn(CollectionUtils.getItemFromList(polarisDataSourceInfo.getInitConn(), size));
            dataInfo.setMinConn(CollectionUtils.getItemFromList(polarisDataSourceInfo.getMinConn(), size));
            dataInfo.setMaxConn(CollectionUtils.getItemFromList(polarisDataSourceInfo.getMaxConn(), size));
            dataInfo.setMaxWaitTime(CollectionUtils.getItemFromList(polarisDataSourceInfo.getMaxWaitTime(), size));
            logger.info("数据源配置{}：{}", size, JSON.toJSONString(dataInfo));
        }
    }



}
